// pages/bleTool/bleTool.js
const blueTool = require('../../utils/bluetool.js')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        logArr: [],
        bleOptTxt: "连接",
        equTypeArr: ["控制盒锁", "陪护椅锁"],
        index: 0,
        inpuCmd:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {},

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    connect: function () {
        // blueTool.BleTool.bleName = 'DKEF356DE8CCC7'
        // blueTool.BleTool.macId = 'EF356DE8CCC7'
        // blueTool.BleTool.bleName = 'C3:32:51:4D:4E:30'
        // blueTool.BleTool.macId = 'C332514D4E30'
        console.log("蓝牙连接mac:", blueTool.BleTool.macId, ",bleName:", blueTool.BleTool.bleName)
        blueTool.BleTool.belLog = this.showLog
        blueTool.BleTool.handleStatus = this.handleConStatus
        if (this.data.bleOptTxt == '连接') {
            blueTool.BleTool.connect()
        } else {
            blueTool.BleTool.closeBLEConnection(blueTool.BleTool.deviceId)
        }

    },
    //发送命令前校验
    sendCmdValidate:function(){
        if (this.data.bleOptTxt == '连接') {            
            return false
        }
        return true
    },
    openLock: function () {
        let validateRst = this.sendCmdValidate()
        if (!validateRst) {
            wx.showToast({
              title: '请先连接',
            })
            return
        }
        if (blueTool.BleTool.curDevIsPeihuyi) {
            blueTool.BleTool.oneMorePhyBleUnlock()
        } else {
            blueTool.BleTool.send85Cmd()
        }
    },
    //发送自定义命令
    sendInputCmd:function() {
        let validateRst = this.sendCmdValidate()
        if (!validateRst) {
            wx.showToast({
              title: '请先连接',
            })
            return
        }
        blueTool.BleTool.sendCustomCmd(this.data.inpuCmd)
    },
    //处理蓝牙连接相关状态信息
    handleConStatus: function (status, data) {
        if (status == blueTool.BleTool.status.CONNECTED) {
            this.setData({
                bleOptTxt: "断开",
            })
        }else if(status == blueTool.BleTool.status.DIS_CONNECT) {
            this.setData({
                bleOptTxt: "连接",
            })
        }
    },
    scanMacInput:function(e) {        
        let that = this
        that.handleScanResult(e.detail.value)  
    },
    cmdInput:function(e) {
       this.data.inpuCmd = e.detail.value
    },
    bindPickerEquTypeChange: function (e) {
        this.setData({
            index: e.detail.value
        })
    },
    //扫码获取mac
    scan: function () {
        let that = this
        wx.scanCode({
            success(res) {
                console.log(res)
                that.handleScanResult(res.result)
            }
        })
    },
    handleScanResult:function(result) {
        let that = this
        if (result.indexOf('http://') != -1) {
            wx.showToast({
                title: '先扫小码',
            })
        } else {
            let scanMac = result.replaceAll(":", "")
            that.setData({
                scanMac: scanMac
            });
            if (that.data.index == 0) {
                blueTool.BleTool.bleName = 'DK' + scanMac
                blueTool.BleTool.macId = scanMac
            } else {
                blueTool.BleTool.bleName = res.result
                blueTool.BleTool.macId = scanMac
            }
        }
    },
    showLog(...vars) {
        let len = vars.length
        let str = ''
        for (let i = 0; i < len; i++) {
            let s = ''
            if (typeof (vars[i]) == 'string') {
                s = vars[i]
            } else {
                s = JSON.stringify(vars[i])
            }
            str += s + ' '
        }
        let curTime = new Date()
        let month = curTime.getMonth() + 1
        let date = curTime.getDate()
        let hour = curTime.getHours()
        let minute = curTime.getMinutes()
        let second = curTime.getSeconds()
        curTime = curTime.getFullYear() + "-" + (month < 10 ? "0" + month : month) + "-" + (date < 10 ? "0" + date : date) + " " + (hour < 10 ? "0" + hour : hour) + ":" + (minute < 10 ? "0" + minute : minute) + ":" + (second < 10 ? "0" + second : second)
        str = "[" + curTime + "]" + str
        console.log(str)
        this.data.logArr.unshift(str);
        this.setData({
            logArr: this.data.logArr
        })
    }
})